package com.svms.save.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.svms.save.cache.PublicCache;
import com.svms.save.cache.PublicCacheService;
import com.svms.save.entitys.SvdaYhb;
import com.svms.save.entitys.SvdaZd;
import com.svms.save.services.SvdaYhService;
import com.svms.save.services.ZdService;
import com.svms.save.utils.SysCode;
import com.svms.save.vo.ResultVo;
import com.svms.save.vo.ZdVo;

/**
 * 
* <p>Title: 字典通用操作服务</p>  
* <p>Description: </p>  
* @author xuyang
* @date 2020年3月16日
 */
@Controller
@RequestMapping("/zd")
public class ZdCommonController {
	@Autowired
	private ZdService zdManager;
	
	private Logger log = LoggerFactory.getLogger(ZdCommonController.class);
	@Autowired
	private SvdaYhService yhManager;
	@Autowired
	private PublicCacheService cacheManager;
	/**
	 * 
	 * <p>Title: 加载字典集合</p>  
	 * <p>Description: </p>
	 * @author xuyang
	 * @date 2020年3月16日
	 * @param model
	 * @param lxjp	字典类型
	 * @return
	 */
	@RequestMapping(value = "/loadZdList",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public ResultVo<SvdaZd> loadZdList(Model model,String lxjp) {
		List<SvdaZd> zdList = PublicCache.getZdListByLxjp(lxjp);
		ResultVo<SvdaZd> result = ResultVo.success("", zdList);
		return result;
	}
	/**
	 * 
	 * <p>Title: 字典翻译</p>  
	 * <p>Description: </p>
	 * @author xuyang
	 * @date 2020年3月16日
	 * @param model
	 * @param lxjp	字典类型
	 * @param zddm	字典代码
	 * @param isJc	是否简称
	 * @return
	 */
	@RequestMapping("/getZdmc")
	@ResponseBody
	public ResultVo<String> getZdmc(Model model,String lxjp,String zddm,Boolean isJc){
		String zdmc = PublicCache.getZdmcByLxjpZddm(lxjp, zddm, isJc);
		ResultVo<String> result = ResultVo.success("", zdmc);
		return result;
	}
	
	@RequestMapping("/toListPage")
	public String toListPage() {
		return "/zd/listPage";
	}
	@RequestMapping("/edit")
	public String toDetail(Model model,ZdVo zd,Long objectid) {
		model.addAttribute("zdLx", zd);
		model.addAttribute("objectid", objectid);
		return "/zd/edit";
	}
	
	@RequestMapping("/zdlxList")
	@ResponseBody
	public ResultVo<List<ZdVo>> zdlxList(){
		ResultVo<List<ZdVo>> res = null;
		try {
			List<ZdVo> list = zdManager.getZd();
			res = ResultVo.success("", list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误!",e);
			res = ResultVo.systemError("系统错误!", null);
		}
		return res;
	}
	@RequestMapping("/zdList")
	@ResponseBody
	public ResultVo<List<SvdaZd>> zdList(String lxjp){
		ResultVo<List<SvdaZd>> res = null;
		try {
			List<SvdaZd> zd = zdManager.findZdByLxjp(lxjp);
			res = ResultVo.success("", zd);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误!",e);
			res = ResultVo.systemError("系统错误!", null);
		}
		return res;
	}
	@RequestMapping("/detailByOid")
	@ResponseBody
	public ResultVo<SvdaZd> detailByOid(Long objectid){
		ResultVo<SvdaZd> res = null;
		try {
			SvdaZd zd = zdManager.detailByOid(objectid);
			res = ResultVo.success("", zd);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误!",e);
			res = ResultVo.systemError("系统错误!", null);
		}
		return res;
	}
	@RequestMapping("/delZd")
	@ResponseBody
	public ResultVo<String> delzd(SvdaZd svdaZd){
		ResultVo res = null;
		try {
			int r = zdManager.deleteZdItem(svdaZd);
			res = ResultVo.success(SysCode.MSG_DEL_OK, r);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误!",e);
			res = ResultVo.systemError("系统错误!", "");
		}
		return res;
	}
	@RequestMapping("/doSave")
	@ResponseBody
	public ResultVo<String> doSave(HttpServletRequest request, SvdaZd svdaZd){
		ResultVo<String> res = null;
		try {
			SvdaYhb yh = yhManager.getCurrentUser(request);
			int r = zdManager.doSave(yh, svdaZd);
			res = ResultVo.success(SysCode.MSG_SAVE_OK, r);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误!",e);
			res = ResultVo.systemError("系统错误!", "");
		}
		return res;
	}
	@RequestMapping("/validateZddm")
	@ResponseBody
	public ResultVo<Boolean> validateZddm(String lxjp,String zddm){
		ResultVo<Boolean> res = null;
		try {
			SvdaZd zd = zdManager.getZdByLxjpZddm(lxjp, zddm);
			if(null == zd) {
				res = ResultVo.success("", Boolean.TRUE);
			}else {
				res = ResultVo.success(SysCode.MSG_ZDDM_ERR, Boolean.FALSE);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误!",e);
			res = ResultVo.systemError("系统错误!", Boolean.FALSE);
		}
		return res;
	}
	@RequestMapping("/reloadCache")
	@ResponseBody
	public ResultVo<String> reloadCache(){
		ResultVo<String> res = null;
		try {
			cacheManager.initZdCache();
			res = ResultVo.success(SysCode.MSG_RECACHE_OK, "");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误!",e);
			res = ResultVo.systemError("系统错误!", "");
		}
		return res;
	}
}
